﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using laba2.Context;

namespace laba2.Repository.DAO
{
    public class GenericDAO <T>: IRepository<T> where T:Entity.Entity 
{
        public void Add(T element)
        {
            using (var context=new Lab3DBContext())
            {
                context.Set<T>().Add(element);
                context.SaveChanges();
            }
        }

        public void Remove(T element)
        {
            using (var context = new Lab3DBContext())
            {
                context.Set<T>().Attach(element);
                context.Set<T>().Remove(element);
                context.SaveChanges();
            }
        }

        public T Get(int id)
        {
            using (var context = new Lab3DBContext())
            {
               return context.Set<T>().Find(id);
             }
        }

        public bool Contains(T element)
        {
            return Get(element.Id)!= null;
        }
}
}
